package com.FileBehavior;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.util.RandomKey;
import org.apache.commons.codec.digest.DigestUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;

import static com.FileBehavior.FileIO.getProjectPath;

public class FileHelp {

    public static File createFile(String fileName) throws IOException {
        File downDir = new File(FileIO.getDownloadFilesPath());
        if(!downDir.exists()){
            downDir.mkdirs();
        }
        File file = new File(FileIO.getDownloadFilesPath() + "/" + fileName);
        if(!file.createNewFile()){
            return null;
        }
        return file;
    }

    public static String relaPathToAbsoPath(String relaPath){
        // 相对路径转绝对路径
        return getProjectPath() + relaPath;
    }

    public static String absoPathToRelaPath(String absoPath) throws IOException {
        // 绝对路径转相对路径
        if(!absoPath.contains(getProjectPath())){
            throw new IOException("绝对路径中没有包含项目根目录");
        }
        return absoPath.replace(getProjectPath(), "");
    }
    public static String getFileMD5(File file) throws IOException {
        // https://blog.csdn.net/sike_java/article/details/78538762
        return DigestUtils.md5Hex(new FileInputStream(file));
    }

    public static String Strings2JSON(String[] strings){
        JSONArray a = new JSONArray(Arrays.asList(strings));
        String b = a.toString();
        return b;
    }
}
